package com.jumi.microservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.constant.ceshi.CeshiEnum;
import com.jumi.microservice.desdzfp.DESDZFP;
import com.jumi.microservice.dto.ceshi.CeshiApiAddRequest;
import com.jumi.microservice.dto.ceshi.CeshiApiListRequest;
import com.jumi.microservice.dto.ceshi.CeshiApiListResponse;
import com.jumi.microservice.entity.Ceshi;
import com.jumi.microservice.mapper.CeshiMapper;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

@Service
public class
CeshiService {
    @Resource
    private CeshiMapper ceshiMapper;

    public static String faPiao() {
        String SendUrl = "http://nnfpbox.nuonuocs.cn/shop/buyer/allow/ecOd/queryElectricKp.action";
        String
                order = "{\n" +
                "    \"identity\":\"2329CC5F90EDAA8208F1F3C72A0CE72A713A9D425CD50CDE\",\n" +
                "    \"orderno\":[\n" +
                "        \"201701041603288333\"\n" +
                "    ]\n" +
                "}";
        order = DESDZFP.encrypt(order);
        HttpClient httpclient = null;
        PostMethod post = null;
        String info = "";
        try {
            httpclient = new HttpClient();
            post = new PostMethod(SendUrl);
//设置编码方式
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//添加参数
            post.addParameter("order", order);
//执行
            httpclient.executeMethod(post);
//接口返回信息
             info = new String(post.getResponseBody(), "UTF-8");
            System.out.println(info);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//关闭连接，释放资源
            post.releaseConnection();
            ((SimpleHttpConnectionManager) httpclient.getHttpConnectionManager()).shutdown();
        }
        return info;
    }

    //开票
    public static String faPiao2(){
        String
                SendUrl = "https://nnfp.jss.com.cn/shop/buyer/allow/cxfKp/cxfServerKpOrderSync.action";
        String order = "{\n" +
                "    \"identity\":\"E7BBBB8716ED3CDD35177DE79370D523F7B63AFA4C3B62A0\",\n" +
                "    \"order\":{\n" +
                "        \"account\":\"\",\n" +
                "        \"address\":\"\",\n" +
                "        \"buyername\":\"个人\",\n" +
                "        \"checker\":\"刘新新\",\n" +
                "        \"salephone\":\"6866889\",\n" +
                "        \"clerk\":\"任新玲\",\n" +
                "        \"saleaddress\":\"山东聚米\",\n" +
                "        \"detail\":[\n" +
                "            {\n" +
                "                \"fphxz\":\"0\",\n" +
                "                \"goodsname\":\"测试商品技术部 绿色\",\n" +
                "                \"hsbz\":\"1\",\n" +
                "                \"num\":\"2\",\n" +
                "                \"price\":\"5.00\",\n" +
                "                \"taxrate\":\"0.13\"\n" +
                "            }\n" +
                "        ],\n" +
                "        \"invoicedate\":\"2021-01-08 17:57:02\",\n" +
                "        \"kptype\":\"1\",\n" +
                "        \"orderno\":\"2101081756154306536\",\n" +
                "        \"payee\":\"王亚群\",\n" +
                "        \"phone\":\"18253389932\",\n" +
                "        \"saletaxnum\":\"91370300321740931T\",\n" +
                "        \"tsfs\":\"1\"\n" +
                "    }\n" +
                "}";
        order = DESDZFP.encrypt(order);
        HttpClient httpclient = null;
        PostMethod post = null;
        String info = "";
        try {
            httpclient = new HttpClient();
            post = new PostMethod(SendUrl);
//设置编码方式
            post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
//添加参数
            post.addParameter("order", order);
//执行
            httpclient.executeMethod(post);
//接口返回信息
            info = new String(post.getResponseBody(), "UTF-8");
            System.out.println(info);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
//关闭连接，释放资源
            post.releaseConnection();
            ((SimpleHttpConnectionManager) httpclient.getHttpConnectionManager()).shutdown();
        }
        return info;
    }


    /**
     * 列表
     */
    public TableDataInfo<CeshiApiListResponse> ceshiList(CeshiApiListRequest ceshiApiListRequest) {
//        //获取当前用户ID
//        Long userId = ceshiApiListRequest.getUid();
//        if (userId == null) {
//            throw new BaseException(CeshiEnum.NO_LOGIN.getCode(), CeshiEnum.NO_LOGIN.getMessage());
//        }
        //String title = ceshiApiListRequest.getTitle();
        /*if (title == null){
            throw new BaseException(CeshiEnum.CESHI_STATUS2.getCode(), CeshiEnum.CESHI_STATUS2.getMessage());
        }
        Integer age = ceshiApiListRequest.getAge();
        if (age == null){
            throw new BaseException(CeshiEnum.CESHI_STATUS2.getCode(), CeshiEnum.CESHI_STATUS2.getMessage());
        }*/

        QueryWrapper<Ceshi> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
       /* queryWrapper.eq("title", ceshiApiListRequest.getTitle());
        queryWrapper.eq("age", ceshiApiListRequest.getAge());*/

        IPage<Ceshi> page = new Page<>(ceshiApiListRequest.getPageNum(), ceshiApiListRequest.getPageSize());
        page = ceshiMapper.selectPage(page, queryWrapper);

        List<Ceshi> ceshis = page.getRecords();

        List<CeshiApiListResponse> result = new ArrayList<>();//有序列表

        for (Ceshi ceshi : ceshis) {
            CeshiApiListResponse ceshiApiListResponse = new CeshiApiListResponse();
            BeanUtils.copyProperties(ceshi, ceshiApiListResponse);

            result.add(ceshiApiListResponse);
        }

        TableDataInfo<CeshiApiListResponse> dataInfo = new TableDataInfo<>();
        dataInfo.setRows(result);
        dataInfo.setTotal(page.getTotal());
        return dataInfo;

    }

    /**
     * 详情
     *
     * @param ceshiApiListRequest
     * @return
     */
    public CeshiApiListResponse ceshiDetail(CeshiApiListRequest ceshiApiListRequest) {
        //获取当前用户ID
        /*Long userId = ceshiApiListRequest.getUid();
        if (userId == null) {
            throw new BaseException(CeshiEnum.NO_LOGIN.getCode(), CeshiEnum.NO_LOGIN.getMessage());
        }*/

        //获取信息ID
        int ceshiId = ceshiApiListRequest.getId();
        //查询信息
        QueryWrapper<Ceshi> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.eq("id", ceshiId);
        Ceshi ceshi = ceshiMapper.selectOne(queryWrapper);
        if (ceshi == null) {
            throw new BaseException(CeshiEnum.CESHI_STATUS1.getCode(), CeshiEnum.CESHI_STATUS1.getMessage());
        }
        //传递信息
        CeshiApiListResponse ceshiApiListResponse = new CeshiApiListResponse();
        BeanUtils.copyProperties(ceshi, ceshiApiListResponse);
        return ceshiApiListResponse;
    }

    /**
     * 添加
     *
     * @param ceshiApiAddRequest
     * @return
     */
    public Boolean addCeshi(CeshiApiAddRequest ceshiApiAddRequest) {
        //获取当前用户ID
      /*  Long userId = ceshiApiAddRequest.getUid();
        if (userId == null) {
            throw new BaseException(CeshiEnum.NO_LOGIN.getCode(), CeshiEnum.NO_LOGIN.getMessage());
        }*/

        Ceshi ceshi = new Ceshi();//entity
        BeanUtils.copyProperties(ceshiApiAddRequest, ceshi);
        // ceshi.setUid(userId);//拼接上uid uid 必须用long长整形， 此处需要单独拼接

        return ceshiMapper.insert(ceshi) > 0;
    }

    /**
     * 修改
     *
     * @param ceshiApiAddRequest
     * @return
     */
    public Boolean editCeshi(CeshiApiAddRequest ceshiApiAddRequest) {
        boolean result = false;
        Integer userId = ceshiApiAddRequest.getId();
        if (userId > 0) {
           /* Ceshi ceshi = ceshiMapper.selectById(userId); //entity  bianliang  mapper.
            if (ceshi == null) {
                throw new BaseException(CeshiEnum.CESHI_STATUS1.getCode(), CeshiEnum.CESHI_STATUS1.getMessage());
            }
            ceshi.setTitle(ceshiApiAddRequest.getTitle());//ceshi 调用的是entity下的，可以用里面的方法
            ceshi.setAge(ceshiApiAddRequest.getAge());
            ceshi.setName(ceshiApiAddRequest.getName());*/
            Ceshi ceshi = new Ceshi();//entity
            BeanUtils.copyProperties(ceshiApiAddRequest, ceshi);

            int row = ceshiMapper.updateById(ceshi);
            if (row > 0) {
                //返回 状态
                result = true;
            }
        }
        return result;
    }

    public static String steams() {
        /*筛选与切片
        filter：过滤流中的某些元素
        limit(n)：获取n个元素
        skip(n)：跳过n元素，配合limit(n)可实现分页
        distinct：通过流中元素的 hashCode() 和 equals() 去除重复元素*/
        Stream<Integer> stream = Stream.of(6, 4, 6, 7, 3, 9, 8, 10, 12, 14, 14);
        Stream<Integer> newStream = stream.filter(s -> s > 5) //6 6 7 9 8 10 12 14 14
                .distinct() //6 7 9 8 10 12 14
                .skip(2) //9 8 10 12 14
                .limit(2); //9 8
        newStream.forEach(System.out::println);
        /*映射
        map：接收一个函数作为参数，该函数会被应用到每个元素上，并将其映射成一个新的元素。
        flatMap：接收一个函数作为参数，将流中的每个值都换成另一个流，然后把所有流连接成一个流。*/
        List<String> list = Arrays.asList("a,b,c", "1,2,3");
        //将每个元素转成一个新的且不带逗号的元素
        //split(",")  === php zhong  explort
        Stream<String> s1 = list.stream().map(s -> s.replaceAll(",", ""));
        s1.forEach(System.out::println); // abc  123

        //直接给数组赋值。比如，String[] strs = new String[]{"张三","李四","王五"};
        //还可以进一步简写为：
        String[] strs = {"张三", "李四", "王五"};
        Arrays.stream(strs).forEach(id -> {
            System.out.println(id);
        });
        /*排序
        sorted()：自然排序，流中元素需实现Comparable接口
        sorted(Comparator com)：定制排序，自定义Comparator排序器*/
        list.stream().sorted().forEach(System.out::println);


        return "sad";
    }

    public static void main(String[] args) {
        faPiao();
    }
}
